package com.cloud.sample.core.exception;

import com.cloud.sample.core.constant.BaseErrorCode;
import com.cloud.sample.core.constant.CoreErrorCode;

import java.text.MessageFormat;

/**
 * @author gjy
 * @since 2021/11/30 0030
 */
public class BaseException extends RuntimeException {

    protected Integer errCode;

    public BaseException() {
    }

    public BaseException(String message) {
        super(message);
        this.errCode = CoreErrorCode.SERVER_ERROR.getCode();
    }

    public BaseException(Integer errCode, String message) {
        super(message);
        this.errCode = errCode;
    }

    public BaseException(BaseErrorCode errorCode, Object... param) {
        this(errorCode.getCode(), format(errorCode.getMessage(), param));
    }

    public BaseException(BaseErrorCode errorCode) {
        this(errorCode.getCode(), errorCode.getMessage());
    }

    public Integer getErrCode() {
        return errCode;
    }

    private static String format(String message, Object... param) {
        return param == null ? message : MessageFormat.format(message, param);
    }
}
